//
//  VPBouncePresentAnimation.m
//  VPPublicUntilitisForPod
//
//  Created by vernepung on 2017/8/25.
//  Copyright © 2017年 vernepung. All rights reserved.
//

#import "VPBouncePresentAnimation.h"
#import "UtilsMacro.h"
@interface VPBouncePresentAnimation()
@property(nonatomic,assign)BOOL isPresenting;
@end

@implementation VPBouncePresentAnimation

- (id)initWithPresenting:(BOOL)presenting{
    self = [super init];
    self.isPresenting = presenting;
    return self;
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return .5f;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIView *containerView = [transitionContext containerView];
    if (self.isPresenting) {
        UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        CGRect finalFrame = [transitionContext finalFrameForViewController:toViewController];
        toViewController.view.frame = CGRectOffset(finalFrame, 0, containerView.bounds.size.height);
//        [containerView addSubview:toViewController.view];
        NSTimeInterval duration = [self transitionDuration:transitionContext];
        
        [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.75 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
//            fromViewController.view.alpha = 0.35;
            toViewController.view.frame = finalFrame;
        } completion:^(BOOL finished) {
//            fromViewController.view.alpha = 1;
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }else{
        UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        
//        CGRect finalFrame = [transitionContext finalFrameForViewController:toViewController];
//        toViewController.view.frame = CGRectOffset(finalFrame, 0, kMainBoundsHeight);
        UIView *containerView = [transitionContext containerView];
        [containerView addSubview:toViewController.view];
//        [containerView sendSubviewToBack:toViewController.view];
        NSTimeInterval duration = [self transitionDuration:transitionContext];
        
        [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.75 initialSpringVelocity:0 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveLinear animations:^{
            CGRect finalframe = fromViewController.view.frame;
            finalframe.origin.y = containerView.bounds.size.height;
            fromViewController.view.frame = finalframe;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }
}

@end
